<?php
/**
 * symfony_sample_proj - TodoController.php
 * Created by PhpStorm.
 * User: mac
 * Date: 5/22/16
 * Time: 08:18
 * Initial version by: mac
 * Initial version creation at: 5/22/16 08:18
 */
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\BrowserKit\Response;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

use Symfony\Component\HttpFoundation\Request;

use AppBundle\Entity\Todo;


class TodoController extends Controller
{
	/**
	 * @Route("/todos", name="todo_list")
	 * @param Request $request
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function listAction(Request $request)
	{
		//die("to do goes here.");
		$todos = $this->getDoctrine()
			->getRepository('AppBundle:Todo')
			->findAll();

		$assignData = array('todos' => $todos);
		return $this->render("todo/index.html.twig", $assignData);
	}


	/**
	 * @Route("/todo/create", name="todo_create")
	 * @param Request $request
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function createAction(Request $request)
	{
		$todo = new Todo();

		$form = $this->createFormBuilder($todo)
			->add('name', TextType::class, array('attr'=>array('class'=>'form-control')))
			->add('category', TextType::class, array('attr'=>array('class'=>'form-control')))
			->add('description', TextareaType::class, array('attr'=>array('class'=>'form-control')))
			->add('priority', ChoiceType::class, array('choices'=>array('Low'=>'low', 'Normal'=>'normal', 'Heigh'=>'heigh'), 'attr'=>array('class'=>'form-control')))
			->add('due_date', DateTimeType::class, array('attr'=>array('class'=>'form-control')))
			->add('save', SubmitType::class, array('label'=>'create & post', 'attr'=>array('class'=>'btn btn-primary')))
			->getForm();

		$form->handleRequest($request);

		if ($form->isSubmitted() && $form->isValid()) {
			//get data
			$name = $form['name']->getData();
			$category = $form['category']->getData();
			$description = $form['description']->getData();
			$priority = $form['priority']->getData();
			$due_date = $form['due_date']->getData();

			$now = new \DateTime('now');

			$todo->setName($name);
			$todo->setCategory($category);
			$todo->setDescription($description);
			$todo->setPriority($priority);
			$todo->setDueDate($due_date);
			$todo->setCreateDate($now);

			$em = $this->getDoctrine()->getManager();
			$em->persist($todo);
			$em->flush();

			$this->addFlash(
				'notice',
				'Todo Added Success'
			);

			return $this->redirectToRoute('todo_list');
		}

		$assignData = array('form'=>$form->createView());
		return $this->render("todo/create.html.twig", $assignData);
	}


	/**
	 * @Route("/todo/details/{id}", name="todo_details")
	 * @param $id
	 * @param Request $request
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function detailsAction($id)
	{
		$todo = $this->getDoctrine()
			->getRepository('AppBundle:Todo')
			->find($id);

		$assignData = array(
			'todo' => $todo
		);

		return $this->render("todo/detail.html.twig", $assignData);
	}


	/**
	 * @Route("/todo/edit/{id}", name="todo_edit")
	 * @param $id
	 * @param Request $request
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function editAction($id, Request $request)
	{
		$todo = $this->getDoctrine()
			->getRepository('AppBundle:Todo')
			->find($id);

		$todo->setName($todo->getName());
		$todo->setCategory($todo->getCategory());
		$todo->setDescription($todo->getDescription());
		$todo->setPriority($todo->getPriority());
		$todo->setDueDate($todo->getDueDate());

		$form = $this->createFormBuilder($todo)
			->add('name', TextType::class, array('attr'=>array('class'=>'form-control')))
			->add('category', TextType::class, array('attr'=>array('class'=>'form-control')))
			->add('description', TextareaType::class, array('attr'=>array('class'=>'form-control')))
			->add('priority', ChoiceType::class, array('choices'=>array('Low'=>'low', 'Normal'=>'normal', 'High'=>'high')))
			->add('due_date', DateTimeType::class, array('attr'=>array('form-control')))
			->add('save', SubmitType::class, array('label'=>'update & post', 'attr'=>array('class'=>'btn btn-primary')))
			->getForm();
		$form->handleRequest($request);

		if ($form->isSubmitted() && $form->isValid()) {
			// get data
			$name = $form['name']->getData();
			$category = $form['category']->getData();
			$description = $form['description']->getData();
			$priority = $form['priority']->getData();
			$due_date = $form['due_date']->getData();

			//instance model
			$em = $this->getDoctrine()->getManager();
			$todo = $em->getRepository('AppBundle:Todo')->find($id);

			$todo->setName($name);
			$todo->setCategory($category);
			$todo->setDescription($description);
			$todo->setPriority($priority);
			$todo->setDueDate($due_date);

			$em->flush();

			$this->addFlash(
				'notice',
				'Todo Updated'
			);

			return $this->redirectToRoute('todo_list');
		}

		$assignData = array(
			'todo'=>$todo,
			'form'=>$form->createView()
		);

		return $this->render("todo/edit.html.twig", $assignData);
	}


	/**
	 * @Route("/todo/delete/{id}", name="todo_delete")
	 * @param $id
	 * @return \Symfony\Component\HttpFoundation\RedirectResponse
	 */
	public function deleteAction($id)
	{

		$em = $this->getDoctrine()->getManager();
		$todo = $em->getRepository('AppBundle:Todo')->find($id);

		$em->remove($todo);
		$em->flush();

		$this->addFlash(
			'notice',
			'Todo Removed success'
		);

		return $this->redirectToRoute('todo_list');
	}

}